/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

/**
 * 
 */
package edu.ucla.cs.typecast.rmi.runtime;

import java.lang.reflect.Method;

import edu.ucla.cs.typecast.rmi.server.TypeCastMetaService;

/**
 *
 * @date Sep 19, 2007
 */
public class RMIUtil {
	private static Method processMethod = null; 
	static public Method getMethodForProcessReturnValue() { 
		if (processMethod == null) { 
			try { 
				Class[] params = new Class[] { Long.class, Object.class }; 		
				processMethod = RMIReturnValueProcessor.class.getMethod("process", params); 
			} catch(Throwable t) { 
				t.printStackTrace(); 
			}
		}
		return processMethod; 
	}
	
	static public boolean isMetaMethod(Method method) { 
		Class type = method.getDeclaringClass(); 
		if (TypeCastMetaService.class.isAssignableFrom(type)) { 
			return true; 
		} else { 
			return false; 
		}
	}
	
	static public Class[] getTargetTypes(Method metaMethod, Object[] args) { 
		if (args == null || args.length == 0) { 
			throw new RuntimeException(metaMethod + " should have at least one argument"); 
		}
		if (args[0] instanceof Class[]) { 
			return (Class[])args[0]; 
		} else { 
			throw new RuntimeException("Metamethod's first argument should have type Class[], it is " + args[0].getClass()); 
		}
	}
}
